﻿using UnityEngine;
using System;

public class CameraManager
{
    private static CameraManager _instance;
    private Camera _uiCamera;

    public CameraManager()
    {
        if (_instance != null)
            throw new Exception("单件实例错误");
        _instance = this;
    }

    public static CameraManager GetInstance()
    {
        return _instance ?? new CameraManager();
    }

    public Camera UICamera { get { return _uiCamera ?? CreateUICamera(); } }

    private Camera CreateUICamera()
    {
        if (_uiCamera != null) return _uiCamera;
        GameObject go = new GameObject("UICamera");
        _uiCamera = go.AddComponent<Camera>();
        _uiCamera.clearFlags = CameraClearFlags.Depth;
        _uiCamera.cullingMask = 1 << LayerMask.NameToLayer("UI");
        _uiCamera.orthographic = true;
        _uiCamera.orthographicSize = 10;
        _uiCamera.nearClipPlane = -120;
        _uiCamera.farClipPlane = 120;
        _uiCamera.depth = 0;
        UnityEngine.Object.DontDestroyOnLoad(go);
        go.AddComponent<AudioListener>();
        return _uiCamera;
    }
}

